package com.tjnu.baiyi.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tjnu.baiyi.entity.TbFriend;
import com.tjnu.baiyi.entity.TbUser;

@Scope("prototype")
@Controller("deleteFriend")
public class DeleteFriend extends BaseAction {
	private static final long serialVersionUID = 4412964990026026731L;

	@Override
	public String execute() throws Exception {
		try {
			System.out.println("DeleteFriend.execute()");
			// 获取当前登录的用户信息
			TbUser currentUser = (TbUser) session.getAttribute("currentUser");
			// 获取要关注的用户Id
			String id = request.getParameter("id").toString();
			System.out.println("id:" + id);
			// 先判断是否已经关注过该好友
			boolean flag = tbFriendDao.validateFriendByCurrentId(
					Integer.parseInt(id), currentUser.getUserId());
			if (flag) {
				// 未关注过：进行关注
			} else {
				// 关注过：
				System.out.println("DeleteFriend.execute() flag");
				TbUser friend = tbUserDao.findById(Integer.parseInt(id));
				TbFriend tf = new TbFriend();
				tf.setTbUserByFriendId(currentUser);
				tf.setTbUserByUserId(friend);
				tbFriendDao.delete(tf);
				session.setAttribute("flag", 1);// 未关注
			}
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}
}
